from sqlalchemy import Column, Integer, String, TIMESTAMP, ForeignKey, Boolean
from sqlalchemy.orm import relationship

from common.fu_model import CoreModel, UUIDStr


# from common.db import Base  # 引入Base类，假设你有一个数据库连接配置

class UiCase(CoreModel):
    __tablename__ = 'ui_case'

    # 用例名称或模块名
    name = Column(String(100), nullable=False)

    # 模块名称
    type = Column(Integer, default=1, comment="0模块，1用例")

    # 父用例ID（外键）
    parent_id = Column(UUIDStr, ForeignKey('ui_case.id'), nullable=True)

    # 标记是否有效
    sys_flag = Column(Boolean, default=True, comment="标记")

    # 子用例的关系
    # parent = relationship("UiCase", remote_side=[id], backref="children")

    # 排序字段
    sort = Column(Integer, default=1, comment="排序")

    steps = relationship('UiCaseStep', back_populates='case', cascade='all, delete-orphan')

    def __repr__(self):
        return f"<Case(id={self.id}, name={self.name}, module_name={self.module_name}, parent_id={self.parent_id})>"

    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

class UiCaseStep(CoreModel):
    __tablename__ = 'ui_case_steps'


    # 步骤描述
    step_description = Column(String(300), nullable=False)
    # 断言
    assertion = Column(String(300), nullable=False)
    # 顺序
    order = Column(Integer, nullable=False)
    # 外键引用 case 表的 id
    case_id = Column(UUIDStr, ForeignKey('ui_case.id'), nullable=False)
    # 断言类型，使用 Enum 来定义类型
    assert_type = Column(String(50), nullable=True)

    # 外键关系
    case = relationship('UiCase', back_populates='steps')

    def __repr__(self):
        return f"<TestStep(id={self.id}, stepDescription={self.step_description}, assertion={self.assertion})>"